Skip to content

Add onLongPress to TextInput - #59148

Open
YevheniiKotyrlo wants to merge 1 commit into
react:mainfrom
YevheniiKotyrlo:feature/text-input-on-long-press
Open

YevheniiKotyrlo wants to merge 1 commit into
react:mainfrom
YevheniiKotyrlo:feature/text-input-on-long-press

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

TextInput already handles its touches through Pressability and exposes onPress, onPressIn and onPressOut, but not onLongPress, so a component cannot tell a held press on a field from a tap. #39701 added onPress on the same grounds.

This passes onLongPress to the Pressability config TextInput already builds. A touch held past the long press delay calls it, and the release that follows calls no onPress, as on Pressable. That release is where TextInput focuses itself, so the input is focused when onLongPress fires instead; without that, a held field with contextMenuHidden on Android ends focused by the platform with no keyboard. Without onLongPress, a held press is still a press.

I need it for a one-time-code field that sets contextMenuHidden on Android and offers its own Paste on a long press.

Changelog:

[GENERAL] [ADDED] - Add onLongPress to TextInput

Test Plan:

Four Fantom cases in TextInput-itest.js under onLongPress: a touch held past the delay calls it once; it focuses the input when it fires, and the release that follows calls no onPress; a press released before the delay still calls onPress and focuses; and without onLongPress a held press still calls onPress and focuses. On main the first two fail (onLongPress is called 0 times; no focus command is sent when the delay passes); with this change the file passes, 38 of 38. yarn flow-check, yarn lint, yarn test-typescript-legacy, yarn build-types (the ReactNativeApi.d.ts snapshot gains onLongPress on TextInputBaseProps), yarn test-generated-typescript and yarn test-ci pass. Mounting and updating 100 TextInputs in a Fantom benchmark, with and without onLongPress, costs the same on main as with this change, within the spread between runs.

On an iPhone 12 Pro Max (iOS 27.0.1) with Xcode 27.1, where CI pins 16.4, RNTester's Debug and Release builds pass. So do 159 of RNTesterUnitTests' 179 tests, with 10 skipped. The 10 that fail also fail on main: four RCTBundleURLProviderTests that read the device's packager setup, RCTConvert_NSColorTests testGenerateFallbacks on iOS 27's colors, and the five RCTBlobManagerTests, whose setUp still sets the bridge key that #59250 stopped synthesizing. Visual regression (ios) reports switch/custom-colors/toggled changed by 11804 px, the same difference main's own runs report on da42db9c90, f9340f07dc and 11b10c0913.

In RNTester's Playground:

function Playground() {
  const [events, setEvents] = React.useState([]);
  const record = name => setEvents(previous => [...previous, name]);
  return (
    <View style={styles.container}>
      <TextInput
        placeholder="Hold me"
        onLongPress={() => record('onLongPress')}
        onPress={() => record('onPress')}
      />
      <TextInput
        contextMenuHidden={true}
        placeholder="Hold me, menu hidden"
        onLongPress={() => record('onLongPress (menu hidden)')}
        onPress={() => record('onPress (menu hidden)')}
      />
      <RNTesterText>{events.join(', ')}</RNTesterText>
    </View>
  );
}

Holding each field for 1.5 s and lifting, in RNTester debug builds on an Android 16 (API 36) emulator: on main nothing is logged while the finger is down and onPress is logged on release; with this change onLongPress is logged while it is down and nothing on release. Both fields end focused with the keyboard up on both builds, and a tap logs onPress on both. Holding a contextMenuHidden field on its text leaves the keyboard down on both builds, because Android's own long press focuses the field first; #59130 stops that long press.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 16, 2026
@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the feature/text-input-on-long-press branch from 39a8fd0 to 3664cc4 Compare October 23, 2026 19:48
TextInput already handles its presses through Pressability and exposes
onPress, onPressIn and onPressOut, but not onLongPress, so a held press
on a TextInput cannot be told apart from a tap.

TextInput now hands onLongPress to Pressability. Once a touch is held
past the long press delay, onLongPress is called, and the release that
follows does not call onPress, as on Pressable. That release is where
TextInput focuses itself, so the input is focused when onLongPress fires
instead; without that, a held field with contextMenuHidden on Android
ends focused by the platform with no keyboard. Without onLongPress a
held press is still a press.
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the feature/text-input-on-long-press branch from 3664cc4 to 77ea664 Compare October 23, 2026 23:35
@github-actions

Copy link
Copy Markdown

Visual regression

1 visual difference(s) at 77ea664136d5. Visual differences need maintainer approval. Review the report, then apply visual-change-approved.

ios switch/custom-colors/toggled: Changed, 11804 px (0.474%)

diff

Expected and Captured
Expected Captured
expected captured

@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as ready for review October 24, 2026 09:44
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant